<?php

class m120410_072501_update_city extends CDbMigration {

	public function safeUp() {
		$this->addColumn('city', 'type', 'varchar(16)');

		$types = array(City::TYPE_REGENCY, City::TYPE_MUNICIPALITY);
		$cities = City::model()->findAll();
		foreach ($cities as $city) {
			$names = explode(' ', $city->name, 2);
			if (array_search($names[0], $types) !== false) {
				$city->type = $names[0];
				$city->name = $names[1];
				$city->save();
			}
		}
	}

	public function safeDown() {
		echo "m120410_072501_update_city does not support migration down.\n";
		return false;
	}

}